package org.framework.edge.builder.docker;

import lombok.extern.slf4j.Slf4j;
import org.framework.edge.builder.domain.model.docker.repository.DockerRepository;
import org.framework.edge.builder.domain.model.docker.repository.DockerRepositoryRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.wu.framework.web.response.Result;

import java.util.List;

@Slf4j
@Configuration
public class InitLoginRepository implements CommandLineRunner {

    private final DockerRepositoryRepository dockerRepositoryRepository;

    private final DockerRuntime dockerRuntime;
    public InitLoginRepository(DockerRepositoryRepository dockerRepositoryRepository, DockerRuntime dockerRuntime) {
        this.dockerRepositoryRepository = dockerRepositoryRepository;
        this.dockerRuntime = dockerRuntime;
    }


    @Override
    public void run(String... args) throws Exception {
        Result<List<DockerRepository>> repositoryRepositoryList = dockerRepositoryRepository.findList(new DockerRepository());
        repositoryRepositoryList.accept(dockerRepositoryList -> {

            for (DockerRepository dockerRepository : dockerRepositoryList) {
                // 登录
                String id = dockerRepository.getId();
                String url = dockerRepository.getUrl();
                String username = dockerRepository.getUsername();
                String password = dockerRepository.getPassword();
                log.info("find docker repository with id: {}", id);
                Result<?> loginRepository = dockerRuntime.loginRepository(url, username, password);
                log.info("login repository: {}", loginRepository);
            }

        });

    }
}
